package org.kl.bf.web.basic;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.kl.bf.entity.basic.NotifyGroupUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.kl.bf.entity.easyui.EasyuiUtils;
import org.kl.bf.service.basic.NotifyGroupUserService;
import org.kl.bf.utils.KLServlets;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.MediaTypes;

@Controller
@RequestMapping(value = "/basic/notifyGroupUser")
public class NotifyGroupUserController extends BasicController {
	@Autowired
	private NotifyGroupUserService notifyGroupUserService;

	@RequiresPermissions("notifyGroup:list")
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "basic/notifyGroupUserList";
	}

	@RequiresPermissions("notifyGroup:list")
	@RequestMapping(value = "dataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String dataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		Page<NotifyGroupUser> notifyGroupUser = notifyGroupUserService.getNotifyGroupUser(searchParams, pageNumber, pageSize, sortType, orderType);
		return EasyuiUtils.buildEasyuiGridJsonString(notifyGroupUser.getContent(), (int) notifyGroupUser.getTotalElements());
	}

	@RequiresPermissions("notifyGroup:update")
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String update(@Valid @ModelAttribute("notifyGroupUser") NotifyGroupUser notifyGroupUser, BindingResult result, HttpSession session) {
		if (result.hasErrors()) {
			return ajaxMsg(bindingMsg(result), false, null);
		}
		try {
			notifyGroupUserService.saveNotifyGroupUser(notifyGroupUser);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("编辑失败[" + e.getMessage() + "]", false, null);
		}
		return ajaxMsg("编辑成功", true, null);
	}

	@RequiresPermissions({ "notifyGroup:delete" })
	@RequestMapping(value = "delete/{id}", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes, HttpSession session) {
		notifyGroupUserService.deleteNotifyGroupUser(id);
		List<NotifyGroupUser> notifyGroupUserList = notifyGroupUserService.getAllNotifyGroupUser();
		for (NotifyGroupUser c : notifyGroupUserList) {
			session.getServletContext().setAttribute(c.getId(), c.getNotifyGroup());
		}
		return ajaxMsg("删除成功", true, null);
	}

	@RequiresPermissions({ "notifyGroup:multidelete" })
	@RequestMapping(value = "multiDelete", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String multiDelete(@RequestParam(value = "ids", defaultValue = "") String ids) {
		String msg = "";
		try {
			msg = notifyGroupUserService.multiDelete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("删除失败:" + e.getMessage(), false, null);
		}
		return ajaxMsg("删除成功:" + msg, true, null);
	}


	@ModelAttribute
	public void getNotifyGroupUser(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("notifyGroupUser", notifyGroupUserService.getNotifyGroupUser(id));
		}
	}
}
